package br.ic.ufal.view;

import br.ufal.ic.dao.FacadeDAO;
import br.ufal.ic.dao.UserDAO;
import br.ufal.ic.user.User;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 * Janela principal que dara acesso a todas as outras.
 * 
 * @author Marcos Paulo
 */
public class MainGUI extends JFrame {

    /**
     * atributo usado para persistir os dados do usario.
     */
    private UserDAO userDAO;
    /**
     * janela onde é possivel administrar todos os dados.
     */
    private AdimGUI adimGUI;
    /**
     * janela usada para criar contas para novos usuarios.
     */
    private UserAccountWizard userAccountWizard;
    /**
     * Mapa das janelas abertas pelos usuarios.
     */
    private Map<User, UserGUI> userGUIMap = new HashMap<User, UserGUI>();

    /** Creates new form GUI */
    public MainGUI() {

        this.userDAO = FacadeDAO.getInstance().getUserDAO();

        adimGUI = new AdimGUI(userDAO);
        adimGUI.setVisible(false);
        adimGUI.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);

        userAccountWizard = new UserAccountWizard(userDAO);
        userAccountWizard.setVisible(false);
        userAccountWizard.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);

        initComponents();

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        passwordField = new javax.swing.JPasswordField();
        nickNameField = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        loginButton = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        AdimMenuItem = new javax.swing.JMenuItem();
        AccountMenuItem = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Versão standalone");
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setResizable(false);

        jLabel1.setFont(new java.awt.Font("Arial", 0, 14));
        jLabel1.setText("Nick name:");

        jLabel2.setFont(new java.awt.Font("Arial", 0, 14));
        jLabel2.setText("Senha:");

        jLabel3.setFont(new java.awt.Font("Arial", 0, 18));
        jLabel3.setText("Faça login");

        loginButton.setText("Logar");
        loginButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loginButtonActionPerformed(evt);
            }
        });
        loginButton.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                loginButtonKeyPressed(evt);
            }
        });

        jMenu1.setText("Menu");

        AdimMenuItem.setText("Administrar");
        AdimMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AdimMenuItemActionPerformed(evt);
            }
        });
        jMenu1.add(AdimMenuItem);

        AccountMenuItem.setText("Criar conta");
        AccountMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AccountMenuItemActionPerformed(evt);
            }
        });
        jMenu1.add(AccountMenuItem);

        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(138, 138, 138)
                .addComponent(jLabel3)
                .addContainerGap(179, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(86, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addGap(29, 29, 29)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(passwordField, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(nickNameField, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 142, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(74, 74, 74))
            .addGroup(layout.createSequentialGroup()
                .addGap(159, 159, 159)
                .addComponent(loginButton, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(158, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addComponent(jLabel3)
                .addGap(45, 45, 45)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(nickNameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(passwordField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel2)))
                .addGap(31, 31, 31)
                .addComponent(loginButton)
                .addContainerGap(76, Short.MAX_VALUE))
        );

        pack();
        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        java.awt.Dimension dialogSize = getSize();
        setLocation((screenSize.width-dialogSize.width)/2,(screenSize.height-dialogSize.height)/2);
    }// </editor-fold>//GEN-END:initComponents

    private void AdimMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AdimMenuItemActionPerformed
//        adimGUI.updateTableRows();
//        adimGUI.setVisible(true);

        JOptionPane.showMessageDialog(null, "Ainda não foi implementado");
    }//GEN-LAST:event_AdimMenuItemActionPerformed

    private void AccountMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AccountMenuItemActionPerformed
//        userAccountWizard.setVisible(true);
        JOptionPane.showMessageDialog(null, "Ainda não foi implementado");
    }//GEN-LAST:event_AccountMenuItemActionPerformed
//    private Map<User, TimeGUI> userGUIMap = new HashMap<User, TimeGUI>();

    /**
     * Metodo que permitira a auteticação dos usuarios.
     * 
     * @param evt
     */
    private void loginButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loginButtonActionPerformed

        User user = userDAO.findByNickName(nickNameField.getText());

        if (user != null && user.getPassword().equals(passwordField.getText())) {
            if (userGUIMap.get(user) == null) {

                UserGUI userGUI = new UserGUI(user);
                userGUI.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
                userGUIMap.put(user, userGUI);
//                TimeGUI timeGUI = new TimeGUI(user);
//                timeGUI.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
//                userGUIMap.put(user, timeGUI);
            }
            userGUIMap.get(user).setVisible(true);
        } else {
            JOptionPane.showMessageDialog(null, "A autenticação falhou!");
        }


    }//GEN-LAST:event_loginButtonActionPerformed

    private void loginButtonKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_loginButtonKeyPressed
        loginButtonActionPerformed(null);
    }//GEN-LAST:event_loginButtonKeyPressed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem AccountMenuItem;
    private javax.swing.JMenuItem AdimMenuItem;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JButton loginButton;
    private javax.swing.JTextField nickNameField;
    private javax.swing.JPasswordField passwordField;
    // End of variables declaration//GEN-END:variables
}
